package com.zzw.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionUtil {

    public static String getErrorMessage(Exception e){
        String messag;
        if(null == e){
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        messag = sw.toString();
        try {
            sw.close();
            pw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return messag;
    }

    public static String GetException(Throwable e) {
        try {
            String s = e.toString()+"\n";
            StackTraceElement[] trace = e.getStackTrace();
            for (StackTraceElement traceElement : trace)
                s = s + "\tat " + traceElement + "\n";

            for (Throwable se : e.getSuppressed())
                s = s + GetException(se);

            Throwable ourCause = e.getCause();
            if (ourCause != null)
                s = s + GetException(ourCause);
            s += "\n";
            return s;
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return "error";
        }

    }
}
